<?php
/**
 * 用户抽奖资格列表
 * User: Administrator
 * Date: 2019\2\23 0023
 * Time: 15:34
 */

namespace app\Lib\Model\LotteryQualification;


use app\Lib\Exception\BlException;
use app\Lib\Exception\SysException;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\UserLotteryQualification;
use app\validate\UserLotteryQualificationValidate;

class LotteryQualificationOperate implements \app\Lib\InterfaceIn\LotteryQualification\LotteryQualificationOperate
{
    private $userMobile;
    private $lotteryType;
    private $num;
    /** @var UserHandle $opUser */
    private $opUser;
    private $table;
    /**
     * @param mixed $userMobile
     */
    public function setUserMobile($userMobile)
    {
        $this->userMobile = $userMobile;
    }

    /**
     * @param mixed $lotteryType
     */
    public function setLotteryType($lotteryType)
    {
        $this->lotteryType = $lotteryType;
    }

    /**
     * @param mixed $num
     */
    public function setNum($num =1)
    {
        $this->num = $num;
    }

    /**
     * @param mixed $opUser
     */
    public function setOpUser(UserHandle $opUser)
    {
        $this->opUser = $opUser;
    }

    private function getTable(){
        if(empty($this->table)){
            $this->table = new UserLotteryQualification();
        }
        return $this->table;
    }

    /**
     * 新增用户抽奖资格
     * @return bool
     * @throws BlException
     */
    public function add()
    {
        $this->checkAdd();
        $model = $this->getTable();
        $data = [
            $model->_create_time=>time(),
            $model->_create_uid=>$this->opUser->getUid(),
            $model->_lottery_type=>$this->lotteryType,
            $model->_num=>$this->num,
            $model->_user_mobile=>$this->userMobile,
        ];
        //入库验证
        $validate = (new UserLotteryQualificationValidate());
        if(!$validate->scene('add')->check($data)){
            throw new BlException($validate->getError());
        }
        //新增入库
        if(!$model->insert($data)){
            throw new BlException('新增用户抽奖资格失败');
        }
        return true;
    }

    /**
     * 验证
     * @return bool
     * @throws BlException
     */
    private function checkAdd(){
        if(empty($this->lotteryType)){
            throw new BlException('抽奖类型不能为空');
        }
        if(empty($this->userMobile)){
            throw new BlException('用户号码不能为空');
        }
        if(empty($this->num)){
            throw new BlException('抽奖次数不能为空');
        }
        if(empty($this->opUser)){
            throw new BlException('获取不到操作人');
        }
        return true;
    }

    /**
     * 减少抽奖次数
     * @return bool
     * @throws SysException
     * @throws BlException
     */
    public function decreaseNum()
    {
        $this->checkFind();
        $model = $this->getTable();
        $result = $model->where(
                        [
                            $model->_user_mobile=>$this->userMobile,
                            $model->_num=>['gt',0],
                            $model->_lottery_type=>$this->lotteryType
                        ]
                    )->dec($model->_num);
        if(!$result){
            throw new SysException('抽奖次数减少失败');
        }
        return true;
    }

    /**
     * @return bool
     * @throws BlException
     */
    private function checkFind(){
        if(empty($this->lotteryType)){
            throw new BlException('抽奖类型不能为空');
        }
        if(empty($this->userMobile)){
            throw new BlException('用户号码不能为空');
        }
        return true;
    }

    /**
     * 验证用户抽奖次数
     * @return bool
     * @throws BlException
     */
    public function checkUserLotteryNum()
    {
        $this->checkFind();
        $model = $this->getTable();
        $result = $model->where(
            [
                $model->_user_mobile=>$this->userMobile,
                $model->_num=>['gt',0],
                $model->_lottery_type=>$this->lotteryType
            ]
        )->value($model->_id);
        return  empty($result)?false:true;
    }


}